package com.sword.controller.buy;

import com.sword.constant.MessageConstant;
import com.sword.page.PageResult;
import com.sword.page.QueryPageBean;
import com.sword.page.Result;
import com.sword.pojo.buy.Buy;
import com.sword.pojo.buy.BuyDetail;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
//import com.sword.service.impl.buy.BuyServiceImpl;
import com.sword.service.impl.buy.BuyDetailImpl;
import org.springframework.web.bind.annotation.ResponseBody;
import com.sword.service.buy.BuyService;

@Controller
@RequestMapping("/buy")
public class BuyController {
    @Autowired
    private BuyService BuyService;
    @Autowired
    private BuyDetailImpl BuyDetailService;

    @RequestMapping("/buy")
    public String buy() {
        return "views/buy/buy";
    }

    @RequestMapping("/buydetail")
    public String buydetail() {
        return "views/buy/buydetail";
    }

    /**
     查询所有退库单
     */
    @RequestMapping("/queryBuy")
    @ResponseBody
    public PageResult queryBuy(@RequestBody QueryPageBean queryPageBean) {
        try {
            return BuyService.queryBuy(queryPageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResult(500, MessageConstant.QUERY_BUY_FAIL, null, null);
        }
    }
    /**
     * 添加退库单功能
     */

    @ResponseBody
    @RequestMapping("/addBuy")
    public Result addBuy(@RequestBody Buy buyorder) {
        try {
            // 获取session保存进数据
          //  String userId = (String) SecurityUtils.getSubject().getSession().getAttribute("loginUser");
           // buyorder.set(userId);
            // 判断是否添加成功
            System.out.println(buyorder);
            Integer flag = BuyService.addBuy(buyorder);

            if (flag <= 0) {
                return new Result(500, MessageConstant.ADD_BUY_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.ADD_BUY_FAIL, null);
        }
        return new Result(200, MessageConstant.ADD_BUY_SUCCESS, null);
    }
    /**
     * 修改退库单
     */
    @ResponseBody
    @RequestMapping("editBuy")
    public Result editBuyorder(@RequestBody Buy buyorder) {
        try {
            Integer flag = BuyService.editBuy(buyorder);
            if (flag <= 0) {
                return new Result(500, MessageConstant.EDIT_BUY_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.EDIT_BUY_FAIL, null);
        }
        return new Result(200, MessageConstant.EDIT_BUY_SUCCESS, null);
    }

    /**
     * 删除退库单
     */

    @ResponseBody
    @RequestMapping("deleteBuy")
    public Result deleteBuy(Integer id) {
        try {
            Integer flag = BuyService.deleteBuy(id);
            if (flag <= 0) {
                return new Result(500, MessageConstant.DELETE_BUY_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.DELETE_BUY_FAIL, null);
        }
        return new Result(200, MessageConstant.DELETE_BUY_SUCCESS, null);
    }

    /**
     查询所有退库明细
     */
    @RequestMapping("/queryBuyDetail")
    @ResponseBody
    public PageResult queryBuyDetail(@RequestBody QueryPageBean queryPageBean) {
        try {
            return BuyDetailService.queryBuyDetail(queryPageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResult(500, MessageConstant.QUERY_BUYDETAIL_FAIL, null, null);
        }
    }
    /**
     * 添加退库明细功能
     */

    @ResponseBody
    @RequestMapping("/addBuyDetail")
    public Result addBuyDetail(@RequestBody BuyDetail buyorderDetail) {
        try {
            // 获取session保存进数据
            String userId = (String) SecurityUtils.getSubject().getSession().getAttribute("loginUser");
            buyorderDetail.setCreateid(userId);
            buyorderDetail.setStatus(0);
            // 判断是否添加成功
            Integer flag = BuyDetailService.addBuyDetail(buyorderDetail);
            if (flag <= 0) {
                return new Result(500, MessageConstant.ADD_BUYDETAIL_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.ADD_BUYDETAIL_FAIL, null);
        }
        return new Result(200, MessageConstant.ADD_BUYDETAIL_SUCCESS, null);
    }
    /**
     * 修改退库明细
     */
    @ResponseBody
    @RequestMapping("editBuyDetail")
    public Result editBuyDetail(@RequestBody BuyDetail buyorderDetail) {
        try {
            Integer flag = BuyDetailService.editBuyDetail(buyorderDetail);
            if (flag <= 0) {
                return new Result(500, MessageConstant.EDIT_BUYDETAIL_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.EDIT_BUYDETAIL_FAIL, null);
        }
        return new Result(200, MessageConstant.EDIT_BUYDETAIL_SUCCESS, null);
    }

    /**
     * 删除退库明细
     */

    @ResponseBody
    @RequestMapping("deleteBuyDetail")
    public Result deleteBuyDetail(Integer id) {
        try {
            Integer flag = BuyDetailService.deleteBuyDetail(id);
            if (flag <= 0) {
                return new Result(500, MessageConstant.DELETE_BUYDETAIL_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.DELETE_BUYDETAIL_FAIL, null);
        }
        return new Result(200, MessageConstant.DELETE_BUYDETAIL_SUCCESS, null);
    }
    /**
     * 审核入库明细
     */
    @ResponseBody
    @RequestMapping("examineBuydetail")
    public Result examineBuydetail(@RequestBody BuyDetail buyDetail) {
        try {
            Integer flag = BuyDetailService.examineBuyDetail(buyDetail);
            if (flag <= 0) {
                return new Result(500, MessageConstant.EDIT_ENTERSTOCKDETAIL_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.EDIT_ENTERSTOCKDETAIL_FAIL, null);
        }
        return new Result(200, MessageConstant.EDIT_ENTERSTOCKDETAIL_SUCCESS, null);
    }
    /**
     * 弃审入库明细
     */
    @ResponseBody
    @RequestMapping("unexamineBuyDetail")
    public Result unexamineBuyDetail(@RequestBody BuyDetail buyDetail) {
        try {
            Integer flag = BuyDetailService.unexamineBuyDetail(buyDetail);
            if (flag <= 0) {
                return new Result(500, MessageConstant.EDIT_ENTERSTOCKDETAIL_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.EDIT_ENTERSTOCKDETAIL_FAIL, null);
        }
        return new Result(200, MessageConstant.EDIT_ENTERSTOCKDETAIL_SUCCESS, null);
    }

}
